package com.gtw.modules.sys.controller;

import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbDividends;
import com.gtw.modules.sys.service.SysDividendsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**分红controller
 * Created by wuye on 2018/1/31.
 */
@RestController
@RequestMapping("/sys/dividends")
public class SysDividendsController extends AbstractController{

    @Autowired
    private SysDividendsService dividendsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:dividends:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbDividends> dividendsList = dividendsService.queryList(query);
        int total = dividendsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(dividendsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dividends:info")
    public R info(@PathVariable("id") Integer id){
        TbDividends dividends = dividendsService.queryObject(id);
        dividends.setDialRatio(dividends.getDialRatio()*100);
        dividends.setIntegralConsumption(dividends.getIntegralConsumption()*100);
        if(dividends.getUserintegralconsumption() != null) {
            dividends.setUserintegralconsumption(dividends.getUserintegralconsumption() * 100);
        }
        return R.ok().put("dividends", dividends);
    }

    /**
     * 保存
     */
    @SysLog("添加分红")
    @RequestMapping("/save")
    @RequiresPermissions("sys:dividends:save")
    public R save(@RequestBody TbDividends dividends){
        dividends.setDialRatio(dividends.getDialRatio()/100);
        dividends.setIntegralConsumption(dividends.getIntegralConsumption()/100);
        if(dividends.getUserintegralconsumption() != null) {
            dividends.setUserintegralconsumption(dividends.getUserintegralconsumption()/100);
        }
        dividends.setCreatetime(new Date());
        dividends.setCreateuser(getUser().getUsername());
        dividends.setUpdatetime(new Date());
        dividends.setUpdateuser(getUser().getUsername());
        dividendsService.save(dividends);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改分红")
    @RequestMapping("/update")
    @RequiresPermissions("sys:dividends:update")
    public R update(@RequestBody TbDividends dividends){
        dividends.setDialRatio(dividends.getDialRatio()/100);
        dividends.setIntegralConsumption(dividends.getIntegralConsumption()/100);
        if(dividends.getUserintegralconsumption() != null) {
            dividends.setUserintegralconsumption(dividends.getUserintegralconsumption()/100);
        }
        dividendsService.update(dividends);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除分红")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dividends:delete")
    public R delete(@RequestBody Integer[] ids){
        dividendsService.deleteBatch(ids);

        return R.ok();
    }
}
